
#include "testSocketServer.h"

CPPUNIT_TEST_SUITE_REGISTRATION (testSocketServer);

void testSocketServer :: setUp (void){
}

void testSocketServer :: tearDown (void) {
}

void testSocketServer :: testMakeSocketServer (void)
{
	socketServer *server = new socketServer(34567, 5);
	socketClient *client = new socketClient("localhost", 34567);
	socketObject *newClient = server->Accept();

	client->SendLine("test");
	CPPUNIT_ASSERT_EQUAL ((string)"test",staticObject::trim( newClient->ReceiveLine()));

	delete newClient;
	delete client;
	delete server;
}

void testSocketServer :: testSocketServerSendNewline (void)
{
	socketServer *server = new socketServer(34567, 5);
	socketClient *client = new socketClient("localhost", 34567);
	socketObject *newClient = server->Accept();

	client->SendLine("test\ntest2\ntest3");
	CPPUNIT_ASSERT_EQUAL ((string)"test",staticObject::trim( newClient->ReceiveLine()));
	CPPUNIT_ASSERT_EQUAL ((string)"test2",staticObject::trim( newClient->ReceiveLine()));
	CPPUNIT_ASSERT_EQUAL ((string)"test3",staticObject::trim( newClient->ReceiveLine()));

	delete newClient;
	delete client;
	delete server;
}

void testSocketServer :: testSocketServerSendAndGetBytes (void)
{
	socketServer *server = new socketServer(34567, 5);
	socketClient *client = new socketClient("localhost", 34567);
	socketObject *newClient = server->Accept();

	client->SendLine("test\ntest2\ntest3");
	CPPUNIT_ASSERT_EQUAL ((string)"test\ntest2\ntest3",staticObject::trim( newClient->ReceiveClearBytes()));

	delete newClient;
	delete client;
	delete server;
}

void testSocketServer :: testSocketServerSendOneByte (void)
{
	socketServer *server = new socketServer(34567, 5);
	socketClient *client = new socketClient("localhost", 34567);
	socketObject *newClient = server->Accept();

	client->SendLine("a");
	CPPUNIT_ASSERT_EQUAL ((string)"a",staticObject::trim( newClient->ReceiveClearBytes()));
	client->SendLine("1");
	CPPUNIT_ASSERT_EQUAL ((string)"1\n",newClient->ReceiveClearBytes());
	client->SendLine("2");
	CPPUNIT_ASSERT_EQUAL ((string)"2\n",newClient->ReceiveClearBytes());

	delete newClient;
	delete client;
	delete server;
}

void testSocketServer :: testSocketServerSendNothing (void)
{
	socketServer *server = new socketServer(34567, 5);
	socketClient *client = new socketClient("localhost", 34567);
	socketObject *newClient = server->Accept();

	client->SendLine("");
	CPPUNIT_ASSERT_EQUAL ((string)"\n",newClient->ReceiveClearBytes());

	delete newClient;
	delete client;
	delete server;
}

void testSocketServer :: testSocketServerSendFile (void)
{
	socketServer *server = new socketServer(34567, 5);
	socketClient *client = new socketClient("localhost", 34567);
	socketObject *newClient = server->Accept();
	std::string content = staticObject::loadFromFile("res/testCompressObject_clear");

	client->SendLine(content);
	CPPUNIT_ASSERT_EQUAL ((string)staticObject::trim(content),staticObject::trim( newClient->ReceiveClearBytes()));

	delete newClient;
	delete client;
	delete server;
}


void testSocketServer :: testSocketServerSendCompressFile (void)
{
	socketServer *server = new socketServer(34567, 5);
	socketClient *client = new socketClient("localhost", 34567);
	socketObject *newClient = server->Accept();
	std::string content = staticObject::loadFromFile("res/testCompressObject_clear.gz");

	client->SendLine(content);
	CPPUNIT_ASSERT_EQUAL ((string)content+"\n",newClient->ReceiveClearBytes());

	delete newClient;
	delete client;
	delete server;
}


void testSocketServer :: testSocketServerSendReallyMuchContent (void)
{
	socketServer *server = new socketServer(34567, 5);
	socketClient *client = new socketClient("localhost", 34567);
	socketObject *newClient = server->Accept();
	std::string fileContent = staticObject::loadFromFile("res/testCompressObject_clear");
	std::string content = "";
	for(int i=0;i<10;++i) {
		content = content + fileContent;
	}
	client->SendLine(content);
	CPPUNIT_ASSERT_EQUAL ((string)content+"\n",newClient->ReceiveClearBytes());

	for(int i=0;i<100;++i) {
		content = content + fileContent;
	}
	client->SendLine(content);
	CPPUNIT_ASSERT_EQUAL ((string)content+"\n",newClient->ReceiveClearBytes());

	delete newClient;
	delete client;
	delete server;
}


